﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Threading;
using System.Collections.Generic;

namespace IWebApp2
{
    /// <summary>
    /// Summary description for ybWebSrvc
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ybWebSrvc : System.Web.Services.WebService
    {
        //wait handles
        private static ManualResetEvent waitDone = new ManualResetEvent(false);
        //variable that stores value sent by Progress.aspx
        private static bool Done=false;

        //name of chat window
        private static string chat_window_name = String.Empty;
        
        //temporary inactive 
        private static Queue<Message> messageQ = new Queue<Message>();
        private static Message message = new Message();
        


        /// <summary>
        /// This web method is called in client script to assign name for chat window
        /// </summary>
        /// <param name="contact">contact name</param>
        [WebMethod]
        public void RunChatBox(String contact)
        {
            chat_window_name=contact;
        }

        /// <summary>
        /// Used like a property. To get the name of chat window
        /// </summary>
        /// <returns>contact name to appear in chat window</returns>
        [WebMethod]
        public  string getChatBoxName()
        {
            return chat_window_name;
        }

        /// <summary>
        /// set null to chat_window_name
        /// </summary>
        [WebMethod]
        public  void clearChatBoxName()
        {
            chat_window_name=String.Empty;
        }

        //Currently inactive
        [WebMethod]
        public static void newMessage(String from,String msg)
        {
            message.From=from;
            message.Body=msg;
            messageQ.Enqueue(message);
        }

        //currently inactive
        [WebMethod]
        public Message getMessage()
        {
            while (messageQ.Count==0)
            {
             
                waitDone.Reset();
               return null;
            }
            waitDone.Set();
            System.Windows.Forms.MessageBox.Show("meron na");
            return messageQ.Dequeue();
        }

        /// <summary>
        /// Call this method to indicate that Progress.aspx has completed its task
        /// </summary>
        [WebMethod]
        public static void ProgressDone()
        {
            Done=true;
        }

        /// <summary>
        /// It works like a property, use it to get the value returned by Progress bar
        /// </summary>
        /// <returns>returns true only. Used in a callback</returns>
        [WebMethod]
        public static bool IsDone()
        {
            //wait for Done to be set to true through continous loop
            while (Done != true)
            {
               waitDone.Reset();
            }
            waitDone.Set();
            return true;
        }

    }


    //Message Class 
    public class Message
    {
        #region variables
        private string body = String.Empty;
        private string from = String.Empty;
        #endregion

        #region Properties
        /// <summary>
        /// sets and gets message body
        /// </summary>
        public string Body
        {
            set
            {
                body = value;
            }
            get
            {
                return body;
            }
        }

        /// <summary>
        /// sets and gets message sender
        /// </summary>
        public string From
        {
            set
            {
                from = value;
            }
            get
            {
                return from;
            }
        }
        #endregion
    }
}
